var app = new Vue({
    el:"#share",
    data () {
        return {
            idcardFront: '', // 身份证正面预览
            idcardBack: '', // 身份证背面预览
            params: {
                realName: '', // 姓名
                mobile: '', // 手机号
                idcardFrontFile: '', // 身份证正面文件
                idcardBackFile: '' // 身份证背面文件
            },
            btnDisabled: false, // 提交按钮是否可用
        }
    },
    methods: {
        // 返回
        goback () {
            window.location.href = 'member_info_authentication.html'
        },
        // 身份证正面预览
        showIdCardFront () {
            let self = this
            let inputDOM = this.$refs.idcardFrontFile;
            // 通过DOM取文件数据
            this.params.idcardFrontFile = inputDOM.files[0];
            var reader = new FileReader();
            reader.onload = function (evt) {
                var srcString = evt.target.result;
                //安卓获取的base64数据无信息头，加之
                if (srcString.substring(5, 10) != "image") {
                    srcString = srcString.replace(/(.{5})/, "$1image/jpeg;");
                }
                self.idcardFront = srcString;
            }
            if (self.params.idcardFrontFile) {
                reader.readAsDataURL(self.params.idcardFrontFile);
            }
        },
        // 身份证正面预览
        showIdCardBack () {
            let self = this
            let inputDOM = this.$refs.idcardBackFile;
            // 通过DOM取文件数据
            this.params.idcardBackFile = inputDOM.files[0];
            var reader = new FileReader();
            reader.onload = function (evt) {
                var srcString = evt.target.result;
                //安卓获取的base64数据无信息头，加之
                if (srcString.substring(5, 10) != "image") {
                    srcString = srcString.replace(/(.{5})/, "$1image/jpeg;");
                }
                self.idcardBack = srcString;
            }
            if (self.params.idcardBackFile) {
                reader.readAsDataURL(self.params.idcardBackFile);
            }
        },
        // 验证数据是否合法
        checkParam() {
            let self = this
            let data = self.params
            for (let i in data) {
                if (!data[i]) {
                    self.toast = self.$createToast({
                        txt: '请填写所有项',
                        type: 'txt'
                    })
                    self.toast.show()
                    return false
                } else if (i === 'mobile') {
                    if (!this.checkMobile(data[i])) {
                        self.toast = self.$createToast({
                            txt: '请填写正确的手机号码！',
                            type: 'txt'
                        })
                        self.toast.show()
                        return false
                    }
                }
            }
            return true
        },
        // 提交按钮
        sumbit () {
            let self = this
            let data = self.params

            let param = new FormData()
            if (self.checkParam()) {
                for (let i in data) {
                    if (i === 'idcardFrontFile') {
                        param.append('file', data[i])
                    } else if (i === 'idcardBackFile') {
                        param.append('file', data[i])
                    } else {
                        param.append(i, data[i])
                    }                    
                }
            } else {
                return
            }
            self.btnDisabled = true
            Axios.post('user/authentication/applySharer.do', param).then(res => {
                if (res.respCode === 'S') {
                    this.toast = this.$createToast({
                        txt: '已经提交至系统审核,稍后会以短信的方式通知您',
                        type: 'txt'
                    })
                    this.toast.show()
                    self.btnDisabled = false
                    setTimeout(() => {
                        window.location.href = 'member_info_authentication.html'
                    }, 2000)
                } else {
                    this.toast = this.$createToast({
                        txt: res.errorMsg,
                        type: 'txt'
                    })
                    this.toast.show()
                    self.btnDisabled = false
                }
            })
        },
        // 验证手机号码
        checkMobile(key) {
            const reg = /^((1[3-9][0-9])+\d{8})$/
            return reg.test(key)
        }
    }
})